package com.lighting.common.feign.search.dto;

import com.lighting.common.feign.search.constant.AggregationType;
import com.lighting.common.feign.search.dto.inside.AggField;
import com.lighting.common.feign.search.dto.inside.GroupField;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Getter
public class AggregationQuery extends Query {

    private List<AggField> aggFields = new ArrayList<>();

    private List<GroupField> groupFields = new ArrayList<>();

    public AggregationQuery() {
    }

    /**
     * 用户无分组单字段统计
     *
     * @param aggType
     * @param aggField
     */
    public AggregationQuery(AggregationType aggType, String aggField) {
        this.aggFields.add(new AggField(aggType.name(), aggField));
    }

    /**
     * 只按分组字段聚合(可用于下拉列表)
     *
     * @param groupFields
     */
    public AggregationQuery(String... groupFields) {
        this.groupFields.addAll(Arrays.asList(groupFields).stream().map(field -> new GroupField(field)).collect(Collectors.toList()));
    }

    /**
     * 添加统计字段
     *
     * @param aggType
     * @param aggField
     */
    public void addAggField(AggregationType aggType, String aggField) {
        this.aggFields.add(new AggField(aggType.name(), aggField));
    }

    /**
     * 添加分组字段
     *
     * @param groupField
     */
    public void addGroupField(GroupField groupField) {
        this.groupFields.add(groupField);
    }

}
